<template>
  <div id="homePage">
    <!-- 搜索框 -->
    <div class="search-bar">
      <a-input-search
        placeholder="从海量图片中搜索"
        v-model:value="searchParams.searchText"
        enter-button="搜索"
        size="large"
        @search="doSearch"
        class="custom-search"
      />
    </div>
    <!-- 分类 + 标签 -->
    <div class="category-container">
      <a-tabs v-model:activeKey="selectedCategory" @change="doSearch" class="category-tabs">
        <a-tab-pane key="all" tab="全部" />
        <a-tab-pane v-for="category in categoryList" :key="category" :tab="category" />
      </a-tabs>
      <a-button type="text" @click="toggleTags" class="tag-toggle-btn">
        <span style="display: flex; align-items: center; gap: 4px">
          <component :is="showTags ? UpOutlined : DownOutlined" />
          {{ showTags ? '收起标签' : '展开标签' }}
        </span>
      </a-button>
    </div>
    <div class="tag-bar" v-show="showTags">
      <span class="tag-title">标签：</span>
      <a-space :size="[0, 8]" wrap>
        <a-checkable-tag
          v-for="(tag, index) in tagList"
          :key="tag"
          v-model:checked="selectedTagList[index]"
          @change="doSearch"
          class="custom-tag"
        >
          {{ tag }}
        </a-checkable-tag>
      </a-space>
    </div>
    <!-- 图片列表 -->
    <PictureList :dataList="dataList" :loading="loading" />
    <a-pagination
      style="text-align: right"
      v-model:current="searchParams.current"
      v-model:pageSize="searchParams.pageSize"
      :total="total"
      @change="onPageChange"
    />
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  listPictureTagCategoryUsingGet,
  listPictureVoByPageUsingPost,
} from '@/api/pictureController'
import { message } from 'ant-design-vue'
import PictureList from '@/components/PictureList.vue'
import { DownOutlined, UpOutlined } from '@ant-design/icons-vue'

const dataList = ref<API.PictureVO[]>()
// 数据
const total = ref(0)
const loading = ref(true)

// 搜索条件
const searchParams = reactive<API.PictureQueryRequest>({
  current: 1,
  pageSize: 50,
  sortField: 'createTime',
  sortOrder: 'descend',
})

// 新增：控制标签显示状态
const showTags = ref(false)

// 新增：切换标签显示状态
const toggleTags = () => {
  showTags.value = !showTags.value
}

// 获取数据
const fetchData = async () => {
  loading.value = true
  // 转换搜索参数
  const params = {
    ...searchParams,
    tags: [] as string[],
  }
  if (selectedCategory.value !== 'all') {
    params.category = selectedCategory.value
  }
  selectedTagList.value.forEach((useTag, index) => {
    if (useTag) {
      params.tags.push(tagList.value[index])
    }
  })
  const res = await listPictureVoByPageUsingPost(params)
  if (res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
  loading.value = false
}

// 页面加载时请求一次
onMounted(() => {
  fetchData()
})

// 分页参数
const onPageChange = (page: number, pageSize: number) => {
  searchParams.current = page
  searchParams.pageSize = pageSize
  fetchData()
}

//搜索
const doSearch = () => {
  searchParams.current = 1
  fetchData()
}

const categoryList = ref<string[]>([])
const selectedCategory = ref<string>('all')
const tagList = ref<string[]>([])
const selectedTagList = ref<boolean[]>([])

// 获取标签和分类选项
const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    // 转换成下拉选项组件接受的格式
    categoryList.value = res.data.data.categoryList ?? []
    tagList.value = res.data.data.tagList ?? []
  } else {
    message.error('加载标签分类失败，' + res.data.message)
  }
}

onMounted(() => {
  getTagCategoryOptions()
})
</script>

<style scoped>
#homePage {
  margin-bottom: 16px;
}

.search-bar {
  max-width: 700px;
  margin: 0 auto 16px;
}

.custom-search :deep(.ant-input) {
  border-radius: 20px 0 0 20px;
  height: 40px;
  font-size: 16px;
}

.category-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 8px;
  padding: 12px 16px; /* 增加左右内边距 */
  border-radius: 8px;
  width: 100%; /* 改为100%宽度 */
  max-width: 800px; /* 保留最大宽度 */
  margin: 0 auto 8px;
  overflow-x: auto; /* 新增：允许横向滚动 */
  white-space: nowrap; /* 新增：防止内容换行 */
  -webkit-overflow-scrolling: touch; /* 新增：iOS平滑滚动 */
}

.category-tabs {
  flex: 1 1 auto; /* 修改：允许收缩 */
  min-width: 0; /* 新增：重要！解决flex溢出问题 */
  margin: 0 12px; /* 减小左右外边距 */
}

.category-tabs :deep(.ant-tabs-nav-wrap) {
  flex-wrap: nowrap; /* 新增：防止导航换行 */
}

.category-tabs :deep(.ant-tabs-nav) {
  margin: 0;
}

.category-tabs :deep(.ant-tabs-tab) {
  font-size: 16px;
  color: #888; /* 改为淡灰色 */
  padding: 8px 16px;
  padding: 8px 12px; /* 减小内边距 */
  flex-shrink: 0; /* 新增：防止标签收缩 */
}

.tag-toggle-btn {
  flex-shrink: 0; /* 新增：防止按钮被压缩 */
  margin-left: 12px;
  background: #f5f5f5;
  border-radius: 4px;
  padding: 4px 8px;
  height: 32px;
}

.tag-toggle-btn span {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
}

.tag-bar {
  padding: 0px 40px;
  max-width: 800px;
  margin: 0 auto 16px;
}

.tag-title {
  font-size: 16px;
  font-weight: 500;
}

.custom-tag {
  font-size: 15px;
  padding: 4px 10px;
  color: #888;
}
</style>
